<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button> 发送请求</button>
    
    <script>
        document.querySelector("button").onclick = function(){
            var xhr = new XMLHttpRequest();

            // 设置请求行
            xhr.open("get","01-timeout.php");
            // 设置请求头:get不需要设置
            // 设置请求体
            xhr.send(null);

            // 设置超时
            xhr.timeout = 2000;
            xhr.ontimeout = function(e){
                console.log(e);
            }

            // 接收响应
            xhr.onreadystatechange = function(){
                if(xhr.status == 200 && xhr.readyState == 4){
                    alert(xhr.responseText);
                }
            }
        }
    </script>
</body>
</html>